package ynu.edu.user.config;

import lombok.Data;
import org.springframework.amqp.core.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@ConfigurationProperties(prefix = "rabbitmq")
@Data
public class RabbitMQConsumerConfig {

    private String queueName;
    private String dlxExchange;
    private String dlxRoutingKey;
    private String exchangeName;
    private String routingKey;
    private String dlxQueueName;

    // 声明业务队列（带死信参数）
    @Bean
    public Queue businessQueue() {
        return QueueBuilder.durable(queueName)
                .withArgument("x-dead-letter-exchange", dlxExchange)
                .withArgument("x-dead-letter-routing-key", dlxRoutingKey)
                .build();
    }

    // 声明业务交换机
    @Bean
    public DirectExchange businessExchange() {
        return new DirectExchange(exchangeName, true, false);
    }

    // 绑定队列和交换机
    @Bean
    public Binding businessBinding() {
        return BindingBuilder.bind(businessQueue())
                .to(businessExchange())
                .with(routingKey);
    }

    // 声明死信队列
    @Bean
    public Queue dlxQueue() {
        return new Queue(dlxQueueName, true, false, false);
    }

    // 声明死信交换机
    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange(dlxExchange, true, false);
    }

    // 绑定死信队列
    @Bean
    public Binding dlxBinding() {
        return BindingBuilder.bind(dlxQueue())
                .to(dlxExchange())
                .with(dlxRoutingKey);
    }
}
